<?php
namespace app\controller\admin;

use app\services\admin\PermissionService;
use app\services\admin\RoleService;
use app\validate\admin\RoleValidate;
use think\App;
use think\Request;
use think\facade\Session;
use think\Validate;

class Role extends Application
{
    protected $roleService;

    public function __construct(App $app = null, RoleService $roleService)
    {
        parent::__construct($app);
        $this->roleService = $roleService;
    }

    public function index()
    {
        return view('admin/role/index');
    }

    public function list(Request $request)
    {
        $status = $request->get('status', 0);
        $search = urldecode($request->get('search', ''));

       return view('admin/role/list', [
            'page_title' => '角色列表',
            'meta' => $this->roleService->getRoleList($status, $search),
        ]);
    }

    public function edit(Request $request)
    {
        $uid = $request->param('id');
        if ($request->isPost() && $request->isAjax()) {
            $data = $request->post();
            if (! $uid) {
                $result = $this->validate($data, RoleValidate::class . '.create'); //验证类场景
            } else {
                $result = $this->validate($data, RoleValidate::class);
            }

            if (true !== $result) {
                return $this->formatResult(-1, $result, [
                    'token' => $request->token('_token')
                ]);
            }

            if ($uid) {
                list($ret, $msg) = $this->roleService->modify($data);
            } else {
                list($ret, $msg) = $this->roleService->save($data);
            }
            if ($ret) {
                return $this->formatResult(0, $msg, ['url' => url('admin/role/list')]);
            }
            return $this->formatResult(1, $msg, [
                'token' => $request->token('_token')
            ]);
        }

        return view('admin/role/edit', [
            'page_title' => $uid ? '编辑角色' : '新增角色',
            'meta' => $this->roleService->getRole($uid),
        ]);
    }

    public function auth(Request $request)
    {
        $uid = $request->param('id');
        if ($request->isPost() && $request->isAjax()) {
            $data = $request->post();
            $result = $this->validate($data, RoleValidate::class . '.auth'); //验证类场景

            if (true !== $result) {
                return $this->formatResult(-1, $result, [
                    'token' => $request->token('_token')
                ]);
            }

            $auth = $request->param('auth');
            $ret = $this->roleService->saveAuth($uid, $auth);

            if ($ret) {
                return $this->formatResult(0, '保存完成', ['url' => url('admin/role/list')]);
            }

            return $this->formatResult(1001, '保存失败', [
                'token' => $request->token('_token')
            ]);
        }

        $meta['permission'] = app(PermissionService::class)->getPermissionGroupsToSelect();
        //dd($meta);
        $meta['role_permission'] = $this->roleService->getRolePermission($uid);

        return view('admin/role/auth', [
            'page_title' => '选择权限',
            'meta' =>$meta,
        ]);
    }

    public function del(Request $request)
    {
        $id = $request->get('id', '');
        if ($id) {
            $res = $this->roleService->del($id);
            if ($res) {
                return $this->formatResult(0, '删除成功');
            }
        }
        return $this->formatResult(1, '删除失败');
    }
}
